<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { equipApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import xmUpload from '@/components/Upload/xm-upload.vue';

  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    priceTypeArr:[] as Array<any>
  })
  interface formQuert {
    shelve_id:number
    equip_id:number
    shelve_name:string
    shelve_image:string
    price_type:number | string
    price:number | string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    equip_id: [{ required: true, trigger: 'blur', message: '请输入装备ID' }],
    shelve_name: [{ required: true, trigger: 'blur', message: '请输入货架装备名称' }],
    shelve_image: [{ required: true, trigger: 'blur', message: '请上传货架装备图片' }],
    price_type: [{ required: true, trigger: 'blur', message: '请选择货架价格类型' }],
    price: [{ required: true, trigger: 'blur', message: '请输入价格' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitSet = false
      if(form.shelve_id) {
        await equipApi.editEquipShelve(form).then(res=>{
          submitSet = true
          ToastSuccess('修改成功')
        }).catch(()=>{
          loading.close()
        })
      }else {
        await equipApi.addEquipShelve(form).then(res=>{
          submitSet = true
          ToastSuccess('新增成功')
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        closeDialog()
        $emit('updateList')
      }
    })
  }
  const openDialog = (query:any = {})=> {
    const { shelve_id=0, shelve_name='', shelve_image='', price_type='', price='', priceTypeArr=[] } = query
    let obj:any = {
      shelve_name,
      shelve_image,
      price_type,
      price
    }
    if(!shelve_id) {
      action.title = '装备货架新增'
    }else {
      obj['shelve_id'] = shelve_id
      action.title = '装备货架编辑'
    }
    action.priceTypeArr = priceTypeArr
    Object.assign(form,obj)
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="8vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="50%"
    :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item v-if="!form.shelve_id" :label-width="formLabelWidth" label="装备ID" prop="equip_id">
          <el-input v-model="form.equip_id" placeholder="请输入装备ID" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="货架装备名称" prop="shelve_name">
          <el-input v-model="form.shelve_name" placeholder="请输入货架装备名称" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="货架装备图片" prop="shelve_image">
          <xmUpload listType="picture-card" :data="{type:27}" v-model:file-url="form.shelve_image"/> 
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="货架价格类型" prop="price_type">
          <el-select v-model="form.price_type" placeholder="请选择货架价格类型">
            <el-option v-for="item in action.priceTypeArr" :label="item.text" :value="item.type" />
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="价格" prop="price">
          <el-input v-model="form.price" type="number" placeholder="请输入价格" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>